package person

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/person"
	personReq "github.com/flipped-aurora/gin-vue-admin/server/model/person/request"
)

type CasPersonsService struct{}

// CreateCasPersons 创建casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) CreateCasPersons(casPersons *person.CasPersons) (err error) {
	err = global.GVA_DB.Create(casPersons).Error
	return err
}

// DeleteCasPersons 删除casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) DeleteCasPersons(ID string) (err error) {
	err = global.GVA_DB.Delete(&person.CasPersons{}, "id = ?", ID).Error
	return err
}

// DeleteCasPersonsByIds 批量删除casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) DeleteCasPersonsByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]person.CasPersons{}, "id in ?", IDs).Error
	return err
}

// UpdateCasPersons 更新casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) UpdateCasPersons(casPersons person.CasPersons) (err error) {
	err = global.GVA_DB.Model(&person.CasPersons{}).Where("id = ?", casPersons.ID).Updates(&casPersons).Error
	return err
}

// GetCasPersons 根据ID获取casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) GetCasPersons(ID string) (casPersons person.CasPersons, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&casPersons).Error
	return
}

// GetCasPersonsInfoList 分页获取casPersons表记录
// Author [yourname](https://github.com/yourname)
func (casPersonsService *CasPersonsService) GetCasPersonsInfoList(info personReq.CasPersonsSearch) (list []person.CasPersons, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&person.CasPersons{})
	var casPersonss []person.CasPersons
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.CaseId != nil {
		db = db.Where("case_id = ?", *info.CaseId)
	}
	if info.Name != nil && *info.Name != "" {
		db = db.Where("name LIKE ?", "%"+*info.Name+"%")
	}
	if info.IdNumber != nil && *info.IdNumber != "" {
		db = db.Where("id_number = ?", *info.IdNumber)
	}
	if info.TownCode != nil && *info.TownCode != "" {
		db = db.Where("town_code = ?", *info.TownCode)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&casPersonss).Error
	return casPersonss, total, err
}
func (casPersonsService *CasPersonsService) GetCasPersonsDataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	caseId := make([]map[string]any, 0)

	global.GVA_DB.Table("cas_cases").Where("deleted_at IS NULL").Select("case_name as label,id as value").Scan(&caseId)
	res["caseId"] = caseId
	return
}
func (casPersonsService *CasPersonsService) GetCasPersonsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
